{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "from SpiderUtils.myUtils import myFakerHeaders\n",
    "from bs4 import BeautifulSoup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "header = {'User-Agent': myFakerHeaders.getFakeHeaders()}\n",
    "p = myFakerHeaders.getFakeProxy()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "data": {
      "text/plain": "200"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "req = requests.get(url=\"https://wallhaven.cc/toplist?page=1\", headers=header, proxies=p)\n",
    "req.status_code"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bs = BeautifulSoup(req.text, parser=\"html.parser\")\n",
    "sections = bs.find_all(\"section\", class_=\"thumb-listing-page\")\n",
    "len(sections)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://wallhaven.cc/w/6olw9x\n",
      "https://wallhaven.cc/w/z8p9rj\n",
      "https://wallhaven.cc/w/3zq6g9\n",
      "https://wallhaven.cc/w/x8eydz\n",
      "https://wallhaven.cc/w/y8q6vl\n",
      "https://wallhaven.cc/w/28152x\n",
      "https://wallhaven.cc/w/2813r9\n",
      "https://wallhaven.cc/w/9mogy1\n",
      "https://wallhaven.cc/w/dp8o7g\n",
      "https://wallhaven.cc/w/e7821r\n",
      "https://wallhaven.cc/w/wq5m26\n",
      "https://wallhaven.cc/w/l3ydp2\n",
      "https://wallhaven.cc/w/z8p2mg\n",
      "https://wallhaven.cc/w/e78pvw\n",
      "https://wallhaven.cc/w/q2rp55\n",
      "https://wallhaven.cc/w/72jldv\n",
      "https://wallhaven.cc/w/g7wel3\n",
      "https://wallhaven.cc/w/pko5qp\n",
      "https://wallhaven.cc/w/k786pm\n",
      "https://wallhaven.cc/w/wq59dx\n",
      "https://wallhaven.cc/w/8ogw7y\n",
      "https://wallhaven.cc/w/1k1jw3\n",
      "https://wallhaven.cc/w/y8ql7g\n",
      "https://wallhaven.cc/w/9mo7kw\n"
     ]
    }
   ],
   "source": [
    "\n",
    "for s in sections:\n",
    "    imgs_url = s.find_all(\"li\")\n",
    "    for img in imgs_url:\n",
    "        print(img.a['href'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [],
   "source": [
    "def get_html(url):\n",
    "    header = {'User-Agent': myFakerHeaders.getFakeHeaders()}\n",
    "    p = myFakerHeaders.getFakeProxy()\n",
    "    req = requests.get(url=url, headers=header, proxies=p)\n",
    "    return req.text"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "anime girls\n",
      "colorful\n",
      "JYANME\n",
      "anime girlscolorfulJYANME\n"
     ]
    }
   ],
   "source": [
    "def is_girls(tags):\n",
    "    if \"girl\" in tags:\n",
    "        return True\n",
    "    elif \"women\" in tags:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "tstr = \"anime girwomenscolorfulJYANME\"\n",
    "print(is_girls(tstr))\n",
    "\n",
    "url = \"https://wallhaven.cc/w/x8eydz\"\n",
    "html = get_html(url)\n",
    "bs = BeautifulSoup(html, parser=\"html.parser\")\n",
    "len(bs.find_all(\"ul\", id=\"tags\"))\n",
    "tags = bs.find_all(\"ul\", id=\"tags\")\n",
    "tags_str = \"\"\n",
    "for tag in tags:\n",
    "    ts = tag.find_all(\"li\")\n",
    "    for t in ts:\n",
    "        print(t.a.text)\n",
    "        tags_str += t.a.text\n",
    "\n",
    "        if is_girls(tags_str):\n",
    "            with open(\"./images/{}.png\".format(), \"wb\") as f:\n",
    "                f.write()\n",
    "\n",
    "\n",
    "print(tags_str)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}